package com.hbnu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author kittychen
 * @since 2025-10-22 16:38
 */
public class JdbcDemo01 {

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            // 1、注册驱动
            // DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            // Class.forName("com.mysql.cj.jdbc.Driver");

            // 2、获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/class2311?serverTimezone=GMT&useSSL=false&characterEncoding=utf8";
            String username = "root";
            String password = "123456";
            connection = DriverManager.getConnection(url, username, password);
            // System.out.println(connection);

            // 3、获取数据库操作对象
            statement = connection.createStatement();

            // 4、执行SQL语句
            // String sql = "insert into tb_user values(1, '张三', 'zhangsan123', 8000.00)";
            String sql = "update tb_user set salary = 80000 where id = 1";
            // String sql = "delete from tb_user where id = 1";
            int rows = statement.executeUpdate(sql);
            // System.out.println(rows == 1 ? "插入成功" : "插入失败");
            System.out.println(rows == 1 ? "更新成功" : "更新失败");
            // System.out.println(rows == 1 ? "删除成功" : "删除失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
